package com.ruoyi.project.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.mapper.ProjectMapper;
import com.ruoyi.project.service.IProjectService;
import com.ruoyi.wbs.domain.ProjectActivity;
import com.ruoyi.wbs.mapper.ProjectActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 项目Service业务层处理
 * 
 * @author zhaogang
 * @date 2024-07-12
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectActivityMapper projectActivityMapper;

    /**
     * 查询项目
     * 
     * @param projectId 项目主键
     * @return 项目
     */
    @Override
    public Project selectProjectByProjectId(Long projectId)
    {
        return projectMapper.selectProjectByProjectId(projectId);
    }

    /**
     * 查询项目列表
     * 
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {

        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int insertProject(Project project)
    {
        SysUser user = getLoginUser().getUser();
        Long userId = user.getUserId();
        int i = projectMapper.insertProject(project);
        if(i>0){
            Long projectId = project.getProjectId();
            projectMapper.insertProjectUser(userId,projectId);
        }
        return i;
    }

    /**
     * 修改项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     * 
     * @param projectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectByProjectIds(Long[] projectIds)
    {
        projectMapper.deleteProjectUserByProjectId(projectIds[0]);
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectByProjectId(Long projectId)
    {
        return projectMapper.deleteProjectByProjectId(projectId);
    }

    /**
     * 根据用户展示相关项目
     * @param userId
     * @return
     */
    @Override
    public List<Project> selectProjectListByUserId(Long userId,Project project) {
        String projectName = project.getProjectName();
        return projectMapper.selectProjectListByUserId(userId,projectName);
    }

    @Override
    public Long selectNameByProject(Project project) {
        return projectMapper.selectNameByProject(project);
    }

    @Override
    public List<Map<String, Object>> checkAll(Long projectId) {
        return projectMapper.checkAllByProjectId(projectId);
    }


    @Override
    public List<Project> selectProjectListAllByUserId() {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return projectMapper.selectProjectListAllByUserId(user.getUserId());
    }

    @Override
    public List<Map<String, Object>> getCascader() {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Project> projectList = projectMapper.selectProjectListAllByUserId(user.getUserId());
        List<ProjectActivity> projectActivityList = projectActivityMapper.selectProjectActivityByOneOfLevel();
        System.out.println("projectActivityList = " + projectActivityList);
        System.out.println("projectList = " + projectList);
        // 将projectList和projectActivityList进行合并,并且根据projectId进行分组
        List<Map<String, Object>> data = projectList.stream().map(p -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", p.getProjectId());
            map.put("label", p.getProjectName());
            List<Map<String, Object>> activityList = projectActivityList.stream()
                    .filter(projectActivity -> projectActivity != null && projectActivity.getProjectId() != null)
                    .filter(projectActivity -> projectActivity.getProjectId().equals(p.getProjectId()))
                    .map(projectActivity -> {
                        Map<String, Object> activityMap = new HashMap<>();
                        activityMap.put("value", projectActivity.getAvtivityId());
                        activityMap.put("label", projectActivity.getActivityName());
                        return activityMap;
                    }).collect(Collectors.toList());
            map.put("children", activityList);
            return map;
        }).collect(Collectors.toList());
        System.out.println("data = " + data);
        return data;
    }

    @Override
    public List<Map<String, Object>> list() {
        SysUser user = getLoginUser().getUser();
        Long userId = user.getUserId();
        return projectMapper.listOfEchars(userId);
    }


}
